﻿package stfei.HorizList {
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import stfei.HorizList.core.HorizList;
	import stfei.HorizList.core.InavBar;
	import stfei.HorizList.core.IparseXml;

	/**
	 * ...
	 * @author Stfei设计 as3
	   项目名称：
	   类应用：列表扩展图片类
	 */
	public class PhotoList extends HorizList {
		private var MCCont:Class
		private var MCBigCont:MovieClip
		private var oldBigLoader:Loader;
		private var detailItemX:Number
		private var detailItemY:Number

		public function set DetailItemX(value:Number):void {
			detailItemX = value;
		}
		
		public function set DetailItemY(value:Number):void 
		{
			detailItemY = value;
		}

		//构造函数，参数：缩略图影片个体类导入，大影片变量
		public function PhotoList(smallMcClass:Class, bigMC:MovieClip){
			MCBigCont = bigMC
			MCCont = smallMcClass
			//创建一个容器实例，用于获取设计尺寸
			var TestMC:MovieClip = new MCCont()
			miniW = TestMC.width
			miniH = TestMC.height
			TestMC = null
		}

		override protected function getParseXml():IparseXml {
			return new inXMLData
		}
		
		override protected function getNavBar():InavBar 
		{
			var barNav:Navigation = new Navigation(_mcNavBar)
			barNav.addEventListener(Navigation.PAGING,setPaging)
			return  barNav
			
		}

		override protected function crateMovieClip(countVal:int):void {
			countVal--
			var ax:Number = regPointArr[countVal][0]
			var ay:Number = regPointArr[countVal][1]
			var PhotoPath:String = DataArr[countVal][0]
			var mcCon:MovieClip = new MCCont()
			mcCon.buttonMode = true
			mcCon.x = ax
			mcCon.y = ay
			mcCon.ID = countVal
			_mcList.addChild(mcCon)
			this.configEvent(mcCon)

			super.timeStop()
			var mcLoader:Loader = new Loader()
			mcLoader.load(new URLRequest(PhotoPath))
			mcCon.addChildAt(mcLoader, 1)
			var preLoader:Preloader = new Preloader(mcLoader, mcCon);
			preLoader.addEventListener(Preloader.LOADOVER, loadover);
		}

		//完成加载事件
		private function loadover(e:Event):void {
			e.currentTarget.removeEventListener(Preloader.LOADOVER, loadover)
			var obj:Loader = e.target.loadMC;
			obj.alpha = 0;
			obj.x = (miniW - obj.width) / 2
			obj.y = (miniH - obj.height) / 2
			obj.addEventListener(Event.ENTER_FRAME, effect);
			super.timeStart()
		}

		//配置影片鼠标事件
		private function configEvent(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(MouseEvent.CLICK, onClick);
			dispatcher.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			dispatcher.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		}

		private function onOut(e:MouseEvent):void {
			MovieClip(e.currentTarget).getChildAt(1).alpha = 1
		}

		private function onOver(e:MouseEvent):void {
			MovieClip(e.currentTarget).getChildAt(1).alpha = 0.5
		}

		private function onClick(e:MouseEvent):void {
			loaderBigPhoto(e.currentTarget.ID)
		}


		//加载大图片。
		private function loaderBigPhoto(ID:int):void {
			var loaderBigMc:Loader = MCBigCont.getChildByName("bigMCLoader") as Loader
			if (loaderBigMc != null){
				MCBigCont.removeChild(loaderBigMc)
			}
			var url:String = DataArr[ID][1]
			var bigMCLoader:Loader = new Loader
			bigMCLoader.name = "bigMCLoader"
			bigMCLoader.load(new URLRequest(url))
			var preLoader:Preloader = new Preloader(bigMCLoader, MCBigCont);
			preLoader.addEventListener(Preloader.LOADOVER, bigMCloadover)
			MCBigCont.addChild(bigMCLoader)
			this.TextSet(ID)
		}

		//设置文本
		private function TextSet(ID:int):void {
			var u:MovieClip = getChildByName("itemDetailContent") as MovieClip
			if (u != null){
				removeChild(u)
			}
			var itemDetailContent:MovieClip = new MovieClip()
			itemDetailContent.x = detailItemX
			itemDetailContent.y = detailItemY
			itemDetailContent.name = "itemDetailContent"
			addChild(itemDetailContent)
			var itemLen:int = DataArr[ID][2].length
			for (var i:int = 0; i < itemLen; i++){
				var labelText:TextField = createTextField(DataArr[ID][2][i][0], 19, 50, 0, 19 * i)
				var bodyText:TextField = createTextField(DataArr[ID][2][i][1], 19, 120, 50, 19 * i)
				itemDetailContent.addChild(labelText)
				itemDetailContent.addChild(bodyText)
			}
		}

		//创建文本控件
		private function createTextField(Text:String, valH:int, valW:int, valX:int, valY:int):TextField {
			var TextFd:TextField = new TextField()
			TextFd.selectable = false
			TextFd.textColor = 0xFFFFFF
			TextFd.text = Text
			TextFd.height = valH
			TextFd.width = valW
			TextFd.x = valX
			TextFd.y = valY
			return TextFd
		}

		override protected function xmlLoadEnding():void {
			loaderBigPhoto(DataArr[0][1])
		}

		private function bigMCloadover(e:Event):void {
			var mc:Loader = Loader(e.target.loadMC)
			mc.alpha = 0
			mc.addEventListener(Event.ENTER_FRAME, effect)
		}


		///图片显示效果
		private function effect(e:Event):void {
			var obj:Object = e.currentTarget;
			obj.alpha += 0.05;
			if (obj.alpha > 1){
				obj.removeEventListener(Event.ENTER_FRAME, effect);
			}
		}


	}

}